package com.ruoyi.crm.service.impl;

import static com.ruoyi.common.utils.DateUtils.getTwoDaysDayDes;
import static com.ruoyi.common.utils.SecurityUtils.getLoginUser;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.crm.domain.dos.*;
import com.ruoyi.crm.domain.dto.ChannelDTO;
import com.ruoyi.crm.domain.dto.ClueDTOQuery;
import com.ruoyi.crm.domain.dto.ReportClueDTO;
import com.ruoyi.crm.domain.dto.TransferDTO;
import com.ruoyi.crm.domain.vo.*;
import com.ruoyi.crm.mapper.ReportDataMapper;
import com.ruoyi.crm.service.ReportDataService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author mr.huang [jie88888822@163.com]
 * @devTime 2022-06-11
 */
@Service
@Slf4j
public class ReportDataServiceImpl implements ReportDataService {
	@Resource
	private ReportDataMapper reportDataMapper;

	@Override
	public IndexVO getReportData(IndexParamsVO indexVO) {
		//获取昵称
		String userName = getLoginUser().getUsername();
		log.info("nickName = ",userName);
		//返回基本数据
		indexVO.setUserName(userName);
		return reportDataMapper.getBaseData(indexVO);
	}

	@Override
	public List<ClueStatisticVO> salesStatistic(IndexParamsVO indexParamsVO) 	{
		log.info("salesStatistic：{}",indexParamsVO);
		List<ClueStatisticVO> clueStatisticVOS = reportDataMapper.salesStatistic(indexParamsVO);

		//已分配线索数 / 全部线索数量 * 100  2/3  2.6666667   2.67
		clueStatisticVOS.stream().forEach(c -> {
			BigDecimal total = new BigDecimal(c.getRadio());
			BigDecimal okClue = new BigDecimal(c.getNum());

			BigDecimal divide = total.divide(okClue, 4, BigDecimal.ROUND_HALF_UP);
			double v = divide.multiply(new BigDecimal(100)).setScale(2).doubleValue();
			c.setRadio(v);
		});
		return clueStatisticVOS;
	}

	@Override
	public List<ClueStatisticVO> businessChangeStatistics(IndexParamsVO indexParamsVO) {
		log.info("businessChangeStatistics：{}",indexParamsVO);
		List<ClueStatisticVO> clueStatisticVOS = reportDataMapper.businessChangeStatistics(indexParamsVO);

		//已分配线索数 / 全部线索数量 * 100  2/3  2.6666667   2.67
		clueStatisticVOS.stream().forEach(clueStatisticVO -> {
			BigDecimal total = new BigDecimal(clueStatisticVO.getRadio());
			BigDecimal okClue = new BigDecimal(clueStatisticVO.getNum());

			BigDecimal divide = total.divide(okClue, 4, BigDecimal.ROUND_HALF_UP);
			double v = divide.multiply(new BigDecimal(100)).setScale(2).doubleValue();
			clueStatisticVO.setRadio(v);
		});
		return clueStatisticVOS;
	}

	@Override
	public AjaxResult getVulnerabilityMap(String beginCreateTime, String endCreateTime) {
		Integer clueCount = reportDataMapper.selectClueCount(beginCreateTime,endCreateTime);
		Integer effectiveClue = reportDataMapper.selectEffectiveClue(beginCreateTime,endCreateTime);
		Integer businessCount = reportDataMapper.selectBusinessCount(beginCreateTime,endCreateTime);
		Integer contractCount = reportDataMapper.selectContractCount(beginCreateTime, endCreateTime);

		HopperVO hopperVO = new HopperVO();
		hopperVO.setCluesNums(clueCount);
		hopperVO.setEffectiveCluesNums(effectiveClue);
		hopperVO.setBusinessNums(businessCount);
		hopperVO.setContractNums(contractCount);
		return AjaxResult.success(hopperVO);
	}

	@Override
	public ContractStatisticsVO cluesStatistics(String beginTime, String endTime) {
		ContractStatisticsVO conVO = new ContractStatisticsVO();
		List<String> axisLs = getTwoDaysDayDes(beginTime, endTime);
		conVO.setxAxis(axisLs);

		//beginTime -> endTime 之间的人数  新增线索   总数
		List<ContractDos> contractCount = reportDataMapper.selectClueTimeCount(beginTime, endTime);
		System.out.println("****************************************contractCount = " + contractCount);
		ContractData contract1 = new ContractData();
		contract1.setName("新增线索数");
		ContractData contract2 = new ContractData();
		contract2.setName("线索总数量");

		int sum = 0;
		Map<Integer,Integer> data1 =new HashMap<>();
		Map<Integer,Integer> data2 =new HashMap<>();
		for(ContractDos c : contractCount){
			for(int i =0;i<axisLs.size();i++){
				if(c.getDateTimes().equals(axisLs.get(i))){
					Integer num = Integer.parseInt(c.getNum().toString());
					data1.put(i,num);
					sum += num;
				} else {
					data1.put(i,0);
				}
				data2.put(i,sum);
			}
		}

		List<Integer> newLs1 = new ArrayList<>();
		List<Integer> sumLs = new ArrayList<>();
		if(contractCount.size()==0 || Objects.isNull(contractCount)){
			for (String i : axisLs) {
				newLs1.add(0);
				sumLs.add(0);
			}
		}else {
			newLs1 = data1.values().stream().map(d -> {
				return d.intValue();
			}).collect(Collectors.toList());

			sumLs = data2.values().stream().map(d -> {
				return d.intValue();
			}).collect(Collectors.toList());
		}
		log.info("contractStatistics.data1 = {}" + data1);
		log.info("contractStatistics.data2 = {}" + data2);

		contract1.setData(newLs1);
		contract2.setData(sumLs);
		ArrayList<ContractData> objects = new ArrayList<>();
		objects.add(contract1);
		objects.add(contract2);
		conVO.setSeries(objects);
		return conVO;
	}

	@Override
	public List<ReportClueDTO> cluesStatisticsList(ClueDTOQuery clueDTOQuery) {
		return reportDataMapper.cluesStatisticsList(clueDTOQuery);
	}

	@Override
	public List<TransferDTO> getTransferDTOS() {
		return reportDataMapper.getTransferDTOS();
	}

	@Override
	public List<ChannelStatisticsDos> channelStatisticsList(String beginTime, String endTime) {
		return reportDataMapper.channelStatisticsList(beginTime, endTime);
	}

	@Override
	public List<DeptStatisticsDos> deptStatisticsList(String beginTime, String endTime) {
		return reportDataMapper.deptStatisticsList(beginTime, endTime);
	}

	@Override
	public List<AscriptionDos> ownerShipStatisticsList(String beginTime, String endTime) {
		return reportDataMapper.ownerShipStatisticsList(beginTime,endTime);
	}

	@Override
	public List<ChannelDTO> activityStatisticsList(String code, String name) {
		return reportDataMapper.activityStatisticsList(code, name);
	}

}
